import 'package:check_app/dao/userInfo_dao.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class MyMobilePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _MyMobilePageState();
}

class _MyMobilePageState extends State<MyMobilePage> {
  bool _loading = false;
  TextEditingController _mobile = new TextEditingController();

  Future<Null> saveMobile(String mobile) async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await UserInfoDao.save({'mobile': mobile});
      setState(() {
        _loading = false;
      });
      if (result.success) {
        Navigator.of(context).pop(mobile);
      } else {
        Fluttertoast.showToast(
          msg: result.message,
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text('修改联系方式'),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              if (_mobile.text != '') {
                saveMobile(_mobile.text.toString());
              }
            },
            child: Container(
              alignment: Alignment.center,
              padding: EdgeInsets.fromLTRB(20, 0, 20, 0),
              child: Text('保存'),
            ),
          ),
        ],
      ),
      body: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: 16),
            padding: EdgeInsets.only(left: 16, right: 16),
            decoration: BoxDecoration(
              color: Color.fromRGBO(255, 255, 255, 1),
              border: Border(
                top: BorderSide(
                  width: 0.5,
                  color: Color.fromRGBO(229, 229, 229, 1),
                ),
                bottom: BorderSide(
                  width: 0.5,
                  color: Color.fromRGBO(229, 229, 229, 1),
                ),
              ),
            ),
            child: TextField(
              autofocus: false,
              controller: _mobile,
              style: TextStyle(
                fontSize: 15.5,
                height: 22.5 / 15.5,
                color: Color.fromRGBO(43, 44, 46, 1),
              ),
              decoration: InputDecoration(
                fillColor: Colors.pink,
                contentPadding: EdgeInsets.fromLTRB(0, 0, 16, 0),
                hintText: "请输入联系方式",
                hintStyle: TextStyle(
                  fontSize: 15.5,
                  height: 22.5 / 15.5,
                  color: Color.fromRGBO(181, 184, 189, 1),
                ),
                border: OutlineInputBorder(
                  borderSide: BorderSide.none,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
